/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ejb.endpointwebsocket.encoder;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringWriter;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
import org.ejb.endpointwebsocket.util.SigEscenario;

/**
 *
 * @author MoisesAntonio
 */
public class EncSigEscenario implements Encoder.Text<SigEscenario>
{

    @Override
    public String encode(SigEscenario object) throws EncodeException {
        
        FileInputStream f;
        StringWriter swriter = new StringWriter();
        try (JsonGenerator jsonGen = Json.createGenerator(swriter)) {
            File fi=new File("c:/pruebas/1.png");
            f = new FileInputStream(fi);
            int size=(int)fi.length();
            byte[] array=new byte[size];
            int n=f.read(array);
            
            //Esto se queda como está que es como codificar la imagen.
            String encodedImage = Base64.encode(array);
            
            jsonGen.writeStartObject()
                .write("escenario", encodedImage)
            .writeEnd();
        } catch (FileNotFoundException ex) {
            //Logger.getLogger(EncEscenario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            //Logger.getLogger(EncEscenario.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return swriter.toString();
    }

    @Override
    public void init(EndpointConfig config) {    }

    @Override
    public void destroy() {    }
    
}
